package com.xzx.mr.index;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @author xinzhixuan
 * @version V1.0
 * @date 2019/8/9 21:37
 */
public class OneIndexMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private String fileName;
    private Text k = new Text();
    private IntWritable v = new IntWritable(1);

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        FileSplit fileSplit = (FileSplit) context.getInputSplit();
        fileName = fileSplit.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // atguigu pingping
        String line = value.toString();
        String[] split = line.split(" ");
        for (String s : split) {
            k.set(s + "--" + fileName);
            context.write(k, v);
        }
    }
}
